<?php

namespace App\Http\Controllers\Entity\sys_categories;


use App\Http\Controllers\Controller;
use App\Http\Requests\Entity\sys_categories\sys_categoriesRequest;
use App\Models\Entity\sys_categories\sys_categories;
use App\Repositories\Entity\sys_categories\sys_categoriesRepository;
use Encore\Admin\Controllers\ModelForm;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Layout\Content;
use Flash;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Input;
use Illuminate\Support\Facades\Route;
use Response;
use Encore\Admin\Tree;

class sys_categoriesController extends Controller
{
    use ModelForm;

    /** @var  sys_categoriesRepository */
    private $sysCategoriesRepository;

    public function __construct(sys_categoriesRepository $sysCategoriesRepo)
    {
        $this->sysCategoriesRepository = $sysCategoriesRepo;
    }

    /**
     * Display a listing of the sys_categories.
     *
     * @return Response
     */
    public function index(Request $request)
    {
        return Admin::content(function (Content $content)  use ($request)  {

            $content->header(__('entity.sys_categories.sys_categories.index_header'));
            $content->description(__('entity.sys_categories.sys_categories.index_description'));

            $content->body($this->grid($request));
        });

        // return Admin::content(function (Content $content) {
        //     $content->header('树状模型');
        //     $content->body(sys_categories::tree());
        // });
    }

    /**
     * Show the form for creating a new sys_categories.
     *
     * @return Response
     */
    public function create(Request $request)
    {
        return Admin::content(function (Content $content)  use ($request) {

            $content->header(__('entity.sys_categories.sys_categories.create_header'));
            $content->description(__('entity.sys_categories.sys_categories.create_description'));

            $content->body($this->form($request));

        });
    }

    /**
     * Store a newly created sys_categories in storage.
     *
     * @param sys_categoriesRequest $request
     *
     * @return Response
     */
    public function store(sys_categoriesRequest $request)
    {
        $input = $request->all();

        $sysCategories = $this->sysCategoriesRepository->create($input);


        Flash::success(__('entity.sys_categories.sys_categories.saved_success'));

        return Input::get('_previous_') ? redirect(Input::get('_previous_')) : redirect(route('entity.sys_categories.sys_categories.index'));

    }

    /**
     * Display the specified sys_categories.
     *
     * @param  int $id
     *
     * @return Response
     */
    public function show($id)
    {
        $sysCategories = $this->sysCategoriesRepository->findWithoutFail($id);

        if (empty($sysCategories)) {
            Flash::error(__('entity.sys_categories.sys_categories.not_found'));

            return redirect(route('entity.sys_categories.sys_categories.index'));
        }

        return view('entity.sys_categories.sys_categories.show')->with('sysCategories', $sysCategories);

    }

    /**
     * Show the form for editing the specified sys_categories.
     *
     * @param  int $id
     *
     * @return Response
     */
    public function edit($id)
    {


        return Admin::content(function (Content $content) use ($id) {

            $content->header(__('entity.sys_categories.sys_categories.edit_header'));
            $content->description(__('entity.sys_categories.sys_categories.edit_description'));

            $content->body($this->form()->edit($id));
        });
    }

    /**
     * Update the specified sys_categories in storage.
     *
     * @param  int              $id
     * @param sys_categoriesRequest $request
     *
     * @return Response
     */
    public function update($id, sys_categoriesRequest $request)
    {
        $sysCategories = $this->sysCategoriesRepository->findWithoutFail($id);

        if (empty($sysCategories)) {
            Flash::error(__('entity.sys_categories.sys_categories.not_found'));

            return redirect(route('entity.sys_categories.sys_categories.index'));
        }
        $input = $request->all();

        $sysCategories = $this->sysCategoriesRepository->update($input, $id);


        Flash::success(__('entity.sys_categories.sys_categories.updated_success'));

        return Input::get('_previous_') ? redirect(Input::get('_previous_')) : redirect(route('entity.sys_categories.sys_categories.index'));
    }

    /**
     * Remove the specified sys_categories from storage.
     *
     * @param  int $id
     *
     * @return Response
     */
    public function destroy($id)
    {
        // 根据 `,` 判断传递过来的是单个id还是多个id
        if (substr_count($id, ',') >= 1) {
            $id = explode(",", $id);
        }

        // 如果是数组则进行批量删除
        if (is_array($id)) {
            if ($flag = $this->sysCategoriesRepository->batchDelete('id', $id)) {

                return response()->json(['message' => __('entity.sys_categories.sys_categories.deleted_success'), 'status' => $flag]);
            } else {
                return response()->json(['message' => __('base.deleted.error'), 'status' => $flag]);
            }
        }

        $sysCategories = $this->sysCategoriesRepository->findWithoutFail($id);

        if (empty($sysCategories)) {
            Flash::error(__('entity.sys_categories.sys_categories.not_found'));

            return redirect(route('entity.sys_categories.sys_categories.index'));
        }

        if ($flag = $this->sysCategoriesRepository->delete($id)) {


            return response()->json(['message' => __('entity.sys_categories.sys_categories.deleted_success'), 'status' => $flag]);
        } else {
            return response()->json(['message' => __('base.deleted.error'), 'status' => $flag]);
        }
    }

    /**
     * [form description]
     * @return {[type]} [description]
     */
    protected function form()
    {
        return Admin::form(sys_categories::class, function (Form $form ) {

            $form->display('id', 'ID');
            $form->text('title', '标签组名称');
            //$form->number('parent_id', '父类ID');
            $form->textarea('description', '描述');
            $form->number('listorder', '排序');
            $form->hidden('parent_id')->default(isset($_REQUEST['parent_id']) ? $_REQUEST['parent_id'] : 0);
            $form->hidden('created_at');
            $form->hidden('updated_at');
            //$form->display('created_at', __('base.created_at'));
            //$form->display('updated_at', __('base.updated_at'));

        });
    }

    /**
     * [grid description]
     * @return {[type]} [description]
     */
    protected function grid(Request $request)
    {

        return Admin::grid(sys_categories::class, function (Grid $grid) {
            // 考虑是否需要scope和排序
            // $grid->model()->orderBy('listorder', 'asc');

            // // 添加按钮
            // if (!\Gate::check('entity.sys_categories.sys_categories.create')) {
            //     $grid->disableCreation();
            // }

            // // 编辑和删除按钮
            // $grid->actions(function ($actions) {
            //     // 编辑按钮
            //     if (!\Gate::check('entity.sys_categories.sys_categories.edit')) {
            //         $actions->disableEdit();
            //     }
            //     // 删除按钮
            //     if (!\Gate::check('entity.sys_categories.sys_categories.destroy')) {
            //         $actions->disableDelete();
            //     }
            // });

            // // 导出按钮
            // if (!\Gate::check('entity.sys_categories.sys_categories.export')) {
            //     $grid->disableExport();
            // }

            // // 批量操作
            // $grid->tools(function ($tools) {
            //     $tools->batch(function ($batch) {
            //         // 批量删除按钮
            //         if (!\Gate::check('entity.sys_categories.sys_categories.batch_destroy')) {
            //             $batch->disableDelete();
            //         }
            //     });
            // });

            // 添加按钮
            if (Admin::user()->cannot('entity.sys_categories.sys_categories.create')) {
                $grid->disableCreation();
            }

            // 编辑和删除按钮
            $grid->actions(function ($actions) {
                //编辑按钮
                if (Admin::user()->cannot('entity.sys_categories.sys_categories.edit')) {
                    $actions->disableEdit();
                }
                // 删除按钮
                if (Admin::user()->cannot('entity.sys_categories.sys_categories.destroy')) {
                    $actions->disableDelete();
                }

                //$actions->prepend('<a href="?parent_id='.$actions->getKey().'"><i class="fa fa-list" title="查看子类"></i></a>');
                //$actions->append('<a href="sys_categories/create?parent_id='.$actions->getKey().'"><i class="fa fa-eye" title="添加子类"></i></a>');

            });

            // 导出按钮
            if (Admin::user()->cannot('entity.sys_categories.sys_categories.export')) {
                $grid->disableExport();
            }

            // 批量操作
            $grid->tools(function ($tools) {
                $tools->batch(function ($batch) {
                    // 批量删除按钮
                    if (Admin::user()->cannot('entity.sys_categories.sys_categories.batch_destroy')) {
                        $batch->disableDelete();
                    }
                });
            });

            $grid->column('id', 'id')->sortable();
            $grid->column('title', '标签组名称')->sortable();
            //$grid->column('parent_id', '父类ID')->sortable();
            $grid->column('description', '描述')->sortable();
            $grid->column('listorder', '排序')->sortable();


            /**
             * 过滤处理
             */
            $grid->filter(function ($filter) {
                // // 禁用id查询框
                // $filter->disableIdFilter();
                $filter->like('title', '分类名称');
                $filter->in('parent_id', '父类ID');
                $filter->between('created_at', '创建时间');

            });

            $grid->disableFilter();
        });
    }
}
